package com.terabits.smartriver.job;

import com.terabits.smartriver.mapper.RegionMapper;
import com.terabits.smartriver.meta.po.Concentration;
import com.terabits.smartriver.meta.po.Treatment;
import com.terabits.smartriver.service.CalculationService;
import com.terabits.smartriver.util.DateUtils;
import com.terabits.smartriver.util.PageData;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.util.List;

import static com.terabits.smartriver.constants.ModelEnum.*;
import static com.terabits.smartriver.util.DateUtils.*;

@Component
public class TreatmentOfWater {
    @Resource
    private CalculationService calculationService;
    @Resource
    private RegionMapper regionMapper;

    @Scheduled(cron = "0 30 1 ? * *")
//    @Scheduled(cron = "0 */1 * * * ?")
    public void volume() {
        List<PageData> projects = regionMapper.selectListSQL("SELECT project_num p FROM project");
        for (PageData pd : projects) {
            String projectNum = pd.getString("p");
            String water = calculationService.treatmentOfWater(getTime(-1), getTime(0), projectNum);
            if (!StringUtils.isEmpty(water) && !"未对项目配置设备".equals(water)) {
                if (water.matches("^[-+]?(([0-9]+)([.]([0-9]+))?|([.]([0-9]+))?)$")) {
                    String yesterday = getDate(null, null, -1);
                    water = String.format("%.3f", Double.parseDouble(water));
                    Treatment treatment = new Treatment();
                    treatment.setProjectNum(projectNum);
                    treatment.setDate(StringToDate(yesterday, "yyyy-MM-dd"));
                    treatment.setWater(water);

                    Double w = Double.parseDouble(water);
                    String codVolume = getVolume(projectNum, base_COD.getCode(), yesterday, w);
                    String nh3nlVolume = getVolume(projectNum, base_NH3N.getCode(), yesterday, w);
                    String pVolume = getVolume(projectNum, base_P.getCode(), yesterday, w);
                    String ssVolume = getVolume(projectNum, base_SS.getCode(), yesterday, w);

                    calculationService.addTreatment(addTreatment(treatment, codVolume, nh3nlVolume, pVolume, ssVolume));
                    System.out.println("###################################数据统计计算完成###################################");
                }
            } else if ("未对项目配置设备".equals(water)) {
                Treatment treatment = new Treatment();
                treatment.setProjectNum(projectNum);
                String yesterday = getDate(null, null, -1);
                treatment.setDate(StringToDate(yesterday, "yyyy-MM-dd"));
                treatment.setWater("0.000");
                calculationService.addTreatment(addTreatment(treatment,
                        "0.00",
                        "0.00",
                        "0.00",
                        "0.00"));
                System.out.println("###################################数据统计计算完成###################################");
            }
        }
    }

    private String getVolume(String projectNum, Integer type, String date, Double water) {
        Concentration concentration;
        concentration = calculationService.getConcentration(projectNum, date, type);
        if (StringUtils.isEmpty(concentration)) {
            //没有最新浓度设置，就是用最新的那一次设置
            concentration = calculationService.getConcentrationOne(projectNum, type);
        }
        if (StringUtils.isEmpty(concentration)) {
            return "0";
        }
        double in = Double.parseDouble(concentration.getInConcentration());
        double out = Double.parseDouble(concentration.getOutConcentration());
        double volume;
        if (concentration != null) {
            volume = water * (in - out);
        } else {
            volume = water * 0.0;
        }
        return volume + "";
    }

    private Treatment addTreatment(Treatment treatment, String codVolume, String nh3nlVolume, String pVolume, String ssVolume) {
        treatment.setCod(String.format("%.2f", Double.parseDouble(codVolume)));
        treatment.setNh3n(String.format("%.2f", Double.parseDouble(nh3nlVolume)));
        treatment.setP(String.format("%.2f", Double.parseDouble(pVolume)));
        treatment.setSs(String.format("%.2f", Double.parseDouble(ssVolume)));
        return treatment;
    }

}
